package com.example.androidlearning.sy7;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.example.androidlearning.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Sy7Activity02 extends AppCompatActivity {

    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sy0702);

        init();
        setListener();
    }

    private void init() {

        listView = findViewById(R.id.sy07_listview);

        String[] list = {"Intent传递数组", "Intent传递集合", "Intent传递对象", "Intent传递Bitmap"};
        ArrayAdapter arrayAdapter = new ArrayAdapter<>(this, R.layout.listview_arry_item, list);
        listView.setAdapter(arrayAdapter);

    }

    private void setListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent it = new Intent();
                ComponentName cn = new ComponentName(Sy7Activity02.this, Sy7Activity020.class);
                it.setComponent(cn);
                Bundle bundle = new Bundle();
                switch (position){
                    case 0:
                        bundle.putStringArray("StringArray", new String[]{"一", "二", "三"});
                        it.putExtra("数组", bundle);
                        break;
                    case 1:
                        //传递复杂些的参数
                        Map<String, Object> map1 = new HashMap<String, Object>();
                        map1.put("key1", "value1");
                        map1.put("key2", "value2");
                        //须定义一个list用于在budnle中传递需要传递的ArrayList<Object>,这个是必须要的
                        ArrayList bundleList = new ArrayList();
                        bundleList.add(map1);
                        bundle.putParcelableArrayList("list",bundleList);
                        it.putExtra("集合", bundle);
                        break;
                    case 2:
                        //使用fastJson转化java对象
                        Book book = new Book();
                        book.setId(123);
                        book.setTitle("Android 疯狂讲义");
                        bundle.putString("book", JSON.toJSONString(book));
                        it.putExtra("对象", bundle);
                        break;
                    case 3:
                        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.little_plane);
                        bundle.putParcelable("bitmap", bitmap);
                        it.putExtra("Bitmap",bundle);
                        break;
                }
                startActivity(it);

            }
        });
    }

}
